//
//  BDChatMessageLayout.m
//  iOS_beboy
//
//  Created by wen on 2018/1/31.
//  Copyright © 2018年 com.8xiong. All rights reserved.
//

#import "BDChatMessageLayout.h"
#import "BDTextLinePositionModifier.h"
#import <YYKit.h>

@implementation BDChatMessageLayout

- (instancetype)initWithMessage:(JMSGMessage *)message{
    if (self = [super init]) {
        _message = message;
        [self _layout];
        [self _calculateHeight];
    }
    return self;
}

- (void)_layout{
    
    /*
     typedef NS_ENUM(NSInteger, JMSGContentType) {
     /// 不知道类型的消息
     kJMSGContentTypeUnknown = 0,
     /// 文本消息
     kJMSGContentTypeText = 1,
     /// 图片消息
     kJMSGContentTypeImage = 2,
     /// 语音消息
     kJMSGContentTypeVoice = 3,
     /// 自定义消息
     kJMSGContentTypeCustom = 4,
     /// 事件通知消息。服务器端下发的事件通知，本地展示为这个类型的消息展示出来
     kJMSGContentTypeEventNotification = 5,
     /// 文件消息
     kJMSGContentTypeFile = 6,
     /// 地理位置消息
     kJMSGContentTypeLocation = 7,
     /// 提示性消息
     kJMSGContentTypePrompt = 8,
     };
     */
    
    switch (_message.contentType) {
        case kJMSGContentTypeText:{
            [self _layoutTextMessage];
        }break;
        case kJMSGContentTypeImage:{
            [self _layoutImageMessage];
        }break;
        case kJMSGContentTypeVoice:{
            [self _layoutVoiceMessage];
        }break;
        case kJMSGContentTypeLocation:{
            [self _layoutLocationMessage];
        }break;
            
        default:
            break;
    }
    
}

- (void)_layoutTextMessage{
    _textMessageLayout = nil;
    JMSGTextContent *textContent = (JMSGTextContent *)_message.content;
    NSMutableAttributedString *text = [self _textWithText:textContent.text
                                                 fontSize:kChatMessageFontSize
                                                textColor:kChatMessageColor];
    if (text.length == 0) return;
    
    BDTextLinePositionModifier *modifier = [BDTextLinePositionModifier new];
    modifier.font = [UIFont fontWithName:@"Heiti SC" size:kChatMessageFontSize];
    modifier.paddingTop = 0;
    modifier.paddingBottom = 0;
    
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(kChatMessageTextMaxWidth, MAXFLOAT);
    container.linePositionModifier = modifier;
    
    _textMessageLayout = [YYTextLayout layoutWithContainer:container text:text];
    if (!_textMessageLayout) return;
    
    CGFloat textHeight = [modifier heightForLineCount:_textMessageLayout.rowCount];
    
    _contentHeight = textHeight + KChatBubbleTopMargin + KChatBubbleBottomMargin;
}

- (void)_layoutImageMessage{
    
}

- (void)_layoutVoiceMessage{
    
}

- (void)_layoutLocationMessage{
    
}

- (void)_calculateHeight{
    
}

///对文本的处理
- (NSMutableAttributedString *)_textWithText:(NSString *)startText
                                    fontSize:(CGFloat)fontSize
                                   textColor:(UIColor *)textColor {
    if (!startText) return nil;
    
    NSMutableString *string = startText.mutableCopy;
    // 字体
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    // 高亮状态的背景
    YYTextBorder *highlightBorder = [YYTextBorder new];
    highlightBorder.insets = UIEdgeInsetsMake(-2, 0, -2, 0);
    highlightBorder.cornerRadius = 3;
//    highlightBorder.fillColor = kBDCellTextHighlightBackgroundColor;
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    text.font = font;
    text.color = textColor;
    
    // 匹配 @用户名
//    NSArray *atResults = [[BDPostHelper regexAt] matchesInString:text.string options:kNilOptions range:text.rangeOfAll];
//    for (NSTextCheckingResult *at in atResults) {
//        if (at.range.location == NSNotFound && at.range.length <= 1) continue;
//        if ([text attribute:YYTextHighlightAttributeName atIndex:at.range.location] == nil) {
//            [text setColor:kBDCellTextHighlightColor range:at.range];
//
//            // 高亮状态
//            YYTextHighlight *highlight = [YYTextHighlight new];
//            [highlight setBackgroundBorder:highlightBorder];
//            // 数据信息，用于稍后用户点击
//            highlight.userInfo = @{kBDLinkAtName : [text.string substringWithRange:NSMakeRange(at.range.location + 1, at.range.length - 1)]};
//            [text setTextHighlight:highlight range:at.range];
//        }
//    }
    
    // 匹配 [表情]
//    NSArray<NSTextCheckingResult *> *emoticonResults = [[BDPostHelper regexEmoticon] matchesInString:text.string options:kNilOptions range:text.rangeOfAll];
//    NSUInteger emoClipLength = 0;
//    for (NSTextCheckingResult *emo in emoticonResults) {
//        if (emo.range.location == NSNotFound && emo.range.length <= 1) continue;
//        NSRange range = emo.range;
//        range.location -= emoClipLength;
//        if ([text attribute:YYTextHighlightAttributeName atIndex:range.location]) continue;
//        if ([text attribute:YYTextAttachmentAttributeName atIndex:range.location]) continue;
//        NSString *emoString = [text.string substringWithRange:range];
//        NSString *imageName = [BDPostHelper emoticonDic][emoString][@"gif"];
//
//        UIImage *image = [BDPostHelper imageNamed:imageName];
//        if (!image) continue;
//
//        NSAttributedString *emoText = [NSAttributedString attachmentStringWithEmojiImage:image fontSize:fontSize];
//        [text replaceCharactersInRange:range withAttributedString:emoText];
//        emoClipLength += range.length - 1;
//    }
    return text;
}

@end
